/**
 * Diamond Exercises for TW101 Module 1
 *
 * Created by dmorti on 12/02/14.
 */
public class DiamondExercises {
    private String name = "Duncan";

    public static void main(String[] args) {
        DiamondExercises exercises = new DiamondExercises();

        System.out.println("Exercise 1: Isosceles Triangle");
        exercises.exercise1(3);

        System.out.println("Exercise 2: Diamond");
        exercises.exercise2(3);

        System.out.println("Exercise 3: Named Diamond");
        exercises.exercise3(3);
    }

    private void exercise1(int n) {
        System.out.println(isoscelesTriangle(n, 0));
    }

    private void exercise2(int n) {
        System.out.print(isoscelesTriangle(n, 0));
        System.out.println(invertedIsoscelesTriangle(n-1, 1));
    }

    private void exercise3(int n) {
        System.out.print(isoscelesTriangle(n-1, 1));
        System.out.println(name);
        System.out.println(invertedIsoscelesTriangle(n-1, 1));
    }

    private String isoscelesTriangle(int n, int offset) {
        String triangle = "";
        for (int i=0; i<n; i++) {
            triangle += repeatString(n-i-1+offset, " ");
            triangle += repeatString(2*i+1, "*");
            triangle += "\n";
        }
        return triangle;
    }

    private String invertedIsoscelesTriangle(int n, int offset) {
        String triangle = "";
        for (int i=n; i>0; i--) {
            triangle += repeatString(n-i+offset, " ");
            triangle += repeatString(i*2-1, "*");
            triangle += "\n";
        }
        return triangle;
    }

    private String repeatString(int n, String s) {
        return new String(new char[n]).replace("\0", s);
    }
}
